<template>
  <div class="login-container">
    <div
      class="form-container"
      :style="formContainerStyle"
    >
      <div style="text-align: center;margin-bottom:15px">
        <img
          src="../../assets/image/carton_logo.png"
          alt=""
          style="width:160px;height:40px"
        >
      </div>
      <el-tabs
        v-model="activeName"
        @tab-click="handleClick"
      >
        <el-tab-pane
          label="密码登录"
          name="login"
        >
          <el-form
            ref="loginForm"
            :model="loginForm"
            :rules="loginRules"
            class="login-form"
            autocomplete="on"
            label-position="left"
          >
            <el-form-item prop="username">
              <span class="svg-container">
                <svg-icon icon-class="user" />
              </span>
              <el-input
                ref="username"
                v-model="loginForm.username"
                placeholder="账号"
                name="username"
                type="text"
                tabindex="1"
                autocomplete="on"
              />
            </el-form-item>
            <el-tooltip
              v-model="capsTooltip"
              content="Caps lock is On"
              placement="right"
              manual
            >
              <el-form-item prop="password">
                <span class="svg-container">
                  <svg-icon icon-class="password" />
                </span>
                <el-input
                  ref="password"
                  v-model="loginForm.password"
                  show-password
                  type="password"
                  placeholder="密码"
                  tabindex="2"
                  @blur="capsTooltip = false"
                  @keyup.enter.native="handleLogin"
                />
              </el-form-item>
            </el-tooltip>
            <div style="margin-left: 250px;cursor: pointer;">
              忘记密码?
            </div>
            <div class="handbutton">
              <el-button
                :loading="loading"
                type="primary"
                round
                style="width:50%;height:47px;"
                @click.native.prevent="handleLogin"
              >
                登录
              </el-button>
              <el-button
                type="primary"
                plain
                style="width:50%;height:47px;"
                @click.native.prevent="handleReset"
              >
                重置
              </el-button>
            </div>
            <div class="handImg">
              <div class="imgs">
                <img
                  src="../../assets/image/qq.png"
                  alt=""
                >
              </div>
              <div class="imgs">
                <img
                  src="../../assets/image/weixin.png"
                  alt=""
                >
              </div>
            </div>
          </el-form>
        </el-tab-pane>
        <el-tab-pane
          label="验证码登录"
          name="loginByPhone"
        >
          <el-form
            ref="passWordLogin"
            :model="phoneLoginForm"
            :rules="phoneLoginRules"
            class="login-form"
            autocomplete="on"
            label-position="left"
          >
            <el-form-item prop="phone">
              <span class="svg-container">
                <svg-icon icon-class="phone" />
              </span>
              <el-input
                ref="phone"
                v-model="phoneLoginForm.phone"
                :placeholder="'请输入手机号'"
                name="phone"
                type="number"
                tabindex="1"
                autocomplete="on"
              />
            </el-form-item>
            <el-form-item
              class="idCode"
              prop="idCode"
            >
              <el-input
                ref="idCode"
                v-model="phoneLoginForm.idCode"
                :placeholder="'请输入验证码'"
                name="idCode"
                type="number"
              />
              <button
                class="sms-btn"
                @click.prevent="getCode"
              >
                <span v-show="!show">获取验证码</span>
                <span
                  v-show="show"
                  class="count"
                >{{ count }} s</span>
              </button>
            </el-form-item>
            <div style="font-size: 12px">
              <a style="margin-left: 5px;color: blue">忘记密码？</a>&nbsp;&nbsp;
              <a style="margin-left: 160px;cursor: auto">没有账号？</a>&nbsp;<a style="color: blue">去注册</a>
            </div>
            <div style="margin-top: 10px;">
              <el-button
                :loading="loading"
                type="primary"
                plain
                style="width:100%;height:40px;"
                @click.native.prevent="smsLogin"
              >登录</el-button>
            </div>
          </el-form>
        </el-tab-pane>
      </el-tabs>
    </div>
    <!-- <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
      auto-complete="on"
      label-position="left"
    >
      <div class="title-container">
        <h3 class="title">用户登入</h3>
      </div>
      <el-form-item prop="username">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="username"
          v-model="loginForm.username"
          placeholder="Username"
          name="username"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>
      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="Password"
          name="password"
          tabindex="2"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
        />
        <span
          class="show-pwd"
          @click="showPwd"
        >
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>
      <el-button
        :loading="loading"
        type="primary"
        style="width:100%;margin-bottom:30px;"
        @click.native.prevent="handleLogin"
      >Login</el-button>
    </el-form> -->
  </div>
</template>

<script>
// import { validUsername } from '@/utils/validate'

export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (value.length === 0) {
        callback(new Error('Please enter the correct user name'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('The password can not be less than 6 digits'))
      } else {
        callback()
      }
    }
    const checkPhoneNumber = (rule, value, callback) => {
      if (!/^((1[3,5,8,7,9][0-9])|(14[5,7])|(17[0,6,7,8])|(19[1,7]))\d{8}$/.test(value) || value.length === 0) {
        callback(new Error('The format of the mobile phone number is incorrect'))
      } else {
        callback()
      }
    }
    const checkPhoneIdCode = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('The idCode can not be less than 6 digits'))
      } else {
        callback()
      }
    }
    return {
      // 密码登录
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      // 手机号登录
      phoneLoginForm: {
        phone: '',
        idCode: ''
      },
      phoneLoginRules: {
        phone: [{ required: true, trigger: 'blur', validator: checkPhoneNumber }],
        idCode: [{ required: true, trigger: 'blur', validator: checkPhoneIdCode }]
      },
      show: false,
      count: 300,
      formContainerStyle: {
        // width: window.innerHeight * 0.95 * 1083 / 1850 * 0.8 + 'px',
        width: 460 + 'px',
        marginTop: window.innerHeight * 0.95 * 0.36 + 'px'
      },
      activeName: 'login',
      loading: false,
      capsTooltip: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    // 页签
    handleClick(tab, event) {
      console.log(tab, event)
    },
    // 密码登录
    // handleLogin() {
    //   this.loginForm.password = this.password
    //   this.$refs.loginForm.validate(valid => {
    //     if (valid) {
    //       this.passwordInput = false
    //       this.$nextTick(() => {
    //         this.loading = true
    //         this.$store.dispatch('user/login', this.loginForm)
    //           .then((res) => {
    //             if (res.message === 'firsLogin') {
    //               this.loading = false
    //               this.dialogVisible = true
    //             } else {
    //               localStorage.setItem('username', this.loginForm.username)
    //               this.$router.push({ path: this.redirect || '/', query: this.otherQuery })
    //               this.loading = false
    //             }
    //           })
    //           .catch(() => {
    //             this.loading = false
    //             this.passwordInput = true
    //           })
    //       })
    //     } else {
    //       console.log('error submit!!')
    //       return false
    //     }
    //   })
    // },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          // this.$store.dispatch('/dashboard', this.loginForm).then(() => {
          //   this.$router.push({ path: this.redirect || '/' })
          //   this.loading = false
          // }).catch(() => {
          //   this.loading = false
          // })
          this.$router.push({
            path: '/dashboard'
            // query: {
            //   workOrderNo
            // }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 重置
    handleReset() {
      this.loginForm.username = ''
      this.loginForm.password = ''
    },
    // 获取验证码
    getCode() {
      if (!/^((1[3,5,8,7,9][0-9])|(14[5,7])|(17[0,6,7,8])|(19[1,7]))\d{8}$/.test(this.phoneLoginForm.phone)) return this.$message.warning('手机号不正确')
      this.$nextTick(() => {
        this.$store.dispatch('user/sendLoginSms', this.phoneLoginForm.phone)
          .then((res) => {
            console.log('验证码', res)
            this.show = true
            this.timer = setInterval(() => {
              this.count--
              if (this.count === 0) {
                this.count = 300
                this.show = false
                clearInterval(this.timer)
              }
            }, 1000)
          })
          .catch(() => {
          })
      })
    },
    // 手机登录
    smsLogin() {
      this.$refs.passWordLogin.validate(valid => {
        if (valid) {
          this.$nextTick(() => {
            this.loading = true
            this.$store.dispatch('user/smsLogin', this.phoneLoginForm)
              .then((res) => {
                if (res.message === 'firsLogin') {
                  this.loading = false
                  this.saasLoginVisible = true
                } else {
                  localStorage.setItem('username', this.phoneLoginForm.phone)
                  this.$router.push({ path: this.redirect || '/', query: this.otherQuery })
                  this.loading = false
                }
              })
              .catch(err => {
                console.log(err)
                this.loading = false
                // this.phoneLoginForm.idCode = ''
              })
          })
          this.$router.push({
            path: '/dashboard'
            // query: {
            //   workOrderNo
            // }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */
$bg: #283443;
$light_gray: #fff;
$cursor: #000000;
@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}
/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    background: #f7f8f7;
    height: 47px;
    width: 85%;
    input {
      background: #f7f8f7;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      height: 47px;
      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px #f7f8f7 inset !important;
        -webkit-text-fill-color: black !important;
      }
    }
  }
  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    // background: $light_gray;
    background: #f7f8f7;
    color: #454545;
    margin-bottom: 20px;
    border-radius: 10px;
    /deep/ .el-icon-view {
      font-size: 20px;
    }
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #f49f36;
.login-container {
  min-height: 100%;
  width: 100%;
  background: url('../../assets/image/loginBackground.jpg') no-repeat;
  background-size: 100% 100%;
  overflow: hidden;
  .form-container {
    position: absolute;
    width: 380px !important;
    margin: 200px auto;
    padding-top: 10px;
    margin-left: 70%;
    margin-top: 7% !important;
    .login-form {
      position: relative;
      width: 100%;
      max-width: 100%;
      overflow: hidden;
    }
    /deep/ .el-tabs {
      padding: 20px;
      border-radius: 10px;
      background: #fffeff;
    }
  }
  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;
    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }
  .hander {
    width: 100%;
    margin-top: 20px;
    margin-bottom: 20px;
  }
  .handbutton {
    display: flex;
    padding-top: 10px;
    margin-top: 5px;
    margin-bottom: 15px;
    .el-button {
      border-radius: 5px;
    }
  }
  .handImg {
    display: flex;
    .imgs {
      cursor: pointer;
      width: 50%;
      text-align: center;
    }
  }
  /deep/ .el-tabs__header {
    margin-bottom: 25px;
  }
  /deep/ .el-tabs__nav {
    width: 100% !important;
  }
  /deep/ .el-tabs__item {
    width: 50%;
    font-size: 16px;
    text-align: center;
  }
  /deep/ .el-tabs__active-bar {
    width: 50% !important;
  }
  /deep/ .el-form-item__error {
    margin-top: 5px !important;
  }
  /deep/ input::-webkit-outer-spin-button,
  /deep/ input::-webkit-inner-spin-button {
    -webkit-appearance: none !important;
  }
  /deep/ input[type='number'] {
    -moz-appearance: textfield !important;
  }
  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }
  .title-container {
    position: relative;
    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
    .set-language {
      color: #fff;
      position: absolute;
      top: 3px;
      font-size: 18px;
      right: 0px;
      cursor: pointer;
    }
  }
  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
  .thirdparty-button {
    position: absolute;
    right: 0;
    bottom: 6px;
  }
  @media only screen and (max-width: 470px) {
    .thirdparty-button {
      display: none;
    }
  }
  .login {
    color: $light_gray;
    margin: 0px auto 50px auto;
    text-align: center;
    font-weight: bold;
    border-bottom: 1px #fff solid;
    .title {
      font-size: 30px;
      color: #ffff;
    }
    p {
      margin-top: 0px;
      font-size: 16px;
      color: #d2d2d2;
    }
    img {
      width: 60px;
      height: 60px;
      margin-top: -10px;
    }
  }
  .idCode {
    position: relative;
  }
  .sms-btn {
    width: 100px;
    height: 20px;
    position: absolute;
    top: 13px;
    right: 5px;
    z-index: 222;
    color: blue;
    font-size: 14px;
    border: none;
    border-left: 1px solid #bababa;
    padding-left: 10px;
    background-color: #f7f8f7;
    cursor: pointer;
  }
}
</style>
